package com.ronghuanet.org.service.impl;

import com.ronghuanet.basic.constants.BaseConstants;
import com.ronghuanet.basic.service.IBaiduAiService;
import com.ronghuanet.basic.util.AjaxResult;
import com.ronghuanet.org.domain.Employee;
import com.ronghuanet.org.domain.Shop;
import com.ronghuanet.org.domain.ShopAuditLog;
import com.ronghuanet.org.mapper.EmployeeMapper;
import com.ronghuanet.org.mapper.ShopAuditLogMapper;
import com.ronghuanet.org.mapper.ShopMapper;
import com.ronghuanet.org.service.IEmployeeService;
import com.ronghuanet.org.service.IShopService;
import com.ronghuanet.basic.service.impl.BaseServiceImpl;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-09-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiService baiduAiService;

    @Autowired
    private ShopAuditLogMapper logMapper;

    @Autowired
    private IEmployeeService employeeService;
    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1 校验
        validate(shop);
        //2 保存店铺信息
        shop.setRegisterTime(new Date());
        shopMapper.save(shop);
        //3 保存店铺管理员信息，但是如果店铺管理员原来就有店铺

        //3.1 方案1（采纳）：直接使用原来的店铺管理员信息，或者删除原来的信息添加新的
        Employee admin = shop.getAdmin();
        String username = admin.getUsername();
        Employee adminBydb = employeeMapper.loadByUsername(username);
        if (adminBydb==null){
            //employeeMapper.save(admin);  //不调用mapper，因为这儿的逻辑不仅要保存管理员，还要保存登录信息，这些操作封装在色磁轭
            admin.setState(BaseConstants.Employee.STATE_UNACITIVE);
            employeeService.add(admin);
        }
        //3.2 方案2：不让入驻，直接登录，在后台添加店铺提交审核
        //4 设置管理员
        shop.setAdminId(adminBydb==null?admin.getId():adminBydb.getId());
        //shopMapper.update(shop);

        //5 进行自动审核，根据审核结果需要修改店铺状态，并且添加审核日志
        String authText = shop.getName()+" "+shop.getAddress();
        AjaxResult ajaxResult = AjaxResult.me();
             /*   baiduAiService
                .audit(authText, null, null);*/
        if (ajaxResult.isSuccess()){
            shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        }else{
            shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        }
        shopMapper.update(shop);

        ShopAuditLog auditLog = new ShopAuditLog();
        auditLog.setState(ajaxResult.isSuccess()?1:0);
        auditLog.setShopId(shop.getId());
        //没有审核人，因为是自动审核，但是有审核时间
        auditLog.setAuditTime(new Date());
        auditLog.setNote(ajaxResult.isSuccess()?"审核通过!":"失败,"+ajaxResult.getMessage());
        logMapper.save(auditLog);

        //发送激活邮件-只有新管理员才发激活邮件
        if (adminBydb==null){}


    }

    private void validate(Shop shop) {
        //1.1 null检验
        if (shop==null)
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (StringUtils.isEmpty(shop.getName()))
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (shop.getAdmin()==null)
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (StringUtils.isEmpty(shop.getAdmin().getUsername())||
                StringUtils.isEmpty(shop.getAdmin().getPassword())|| StringUtils.isEmpty(shop.getAdmin().getComfirmPassword()))
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        //1.2 两次密码比对
        if (!shop.getAdmin().getPassword().trim().equals(shop.getAdmin().getComfirmPassword().trim()))
            throw new RuntimeException("两次密码不一致");

        //1.3 店铺是否存在
        Shop shopByDb = shopMapper.loadByName(shop.getName());
        if (shopByDb!=null)
            throw new RuntimeException("该店铺已经存在！");
    }
}
